//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "GlassesBridge.h"

//---------------------------------------------------------------------------

#pragma package(smart_init)

TGlassesBridge :: TGlassesBridge( TDatabase * database)
{
   sequence = new TSequence( database);

   qryGetFirms = new TQuery( NULL);
   qryGetFirms->DatabaseName = database->DatabaseName;
   BuildGetFirmsQuery();

   qryGetModels = new TQuery( NULL);
   qryGetModels->DatabaseName = database->DatabaseName;
   BuildGetModelsQuery();

   qryAdd = new TQuery( NULL);
   qryAdd->DatabaseName = database->DatabaseName;
   qryAddModel = new TQuery( NULL);
   qryAddModel->DatabaseName = database->DatabaseName;
   BuildAddQuery();
   
   qryDelete = new TQuery( NULL);
   qryDelete->DatabaseName = database->DatabaseName;
   qryDeleteModel = new TQuery( NULL);
   qryDeleteModel->DatabaseName = database->DatabaseName;
   BuildDeleteQuery();

   qryGetFirmByName = new TQuery( NULL);
   qryGetFirmByName->DatabaseName = database->DatabaseName;
   BuildGetFirmByName();

   qryGetModelByName = new TQuery( NULL);
   qryGetModelByName->DatabaseName = database->DatabaseName;
   BuildGetModelByName();
}

TGlassesBridge :: ~TGlassesBridge()
{
   delete qryDeleteModel;
   delete qryDelete;
   delete qryAddModel;
   delete qryAdd;
   delete qryGetModels;
   delete qryGetFirms;
   delete qryGetFirmByName;
   delete qryGetModelByName;
   delete sequence;
}

void TGlassesBridge :: BuildGetFirmsQuery()
{
   qryGetFirms->SQL->Add( "SELECT * FROM GLASSESFIRMS");
   qryGetFirms->SQL->Add( "ORDER BY NAME");
}

void TGlassesBridge :: BuildGetModelsQuery()
{
   qryGetModels->SQL->Add( "SELECT * FROM GLASSESMODELS WHERE FIRMID = :prmFirmID");
   qryGetModels->SQL->Add( "ORDER BY MODEL");
}

void TGlassesBridge :: BuildAddQuery()
{
   qryAdd->SQL->Add( "INSERT INTO GLASSESFIRMS VALUES( :prmID, :prmName, :prmAddress, :prmPhone)");
   qryAddModel->SQL->Add( "INSERT INTO GLASSESMODELS VALUES( :prmFirmID, :prmModel, :prmPrice, :prmBought, :prmSold)");
}

void TGlassesBridge :: BuildDeleteQuery()
{
   qryDelete->SQL->Add( "DELETE FROM GLASSESFIRMS WHERE ID = :prmID");
   qryDeleteModel->SQL->Add( "DELETE FROM GLASSESMODELS WHERE FIRMID = :prmFirmID");
}

void TGlassesBridge :: BuildGetFirmByName()
{
   qryGetFirmByName->SQL->Add("SELECT * FROM GLASSESFIRMS");
   qryGetFirmByName->SQL->Add("WHERE NAME = :prmName");
}

void TGlassesBridge :: BuildGetModelByName()
{
   qryGetModelByName->SQL->Add("SELECT * FROM GLASSESMODELS");
   qryGetModelByName->SQL->Add("WHERE FIRMID = :prmFirmID");
   qryGetModelByName->SQL->Add("AND MODEL = :prmModelName");
}

void SetFirm(TQuery* query, TGlassesFirm* firm)
{
   firm->id   = query->FieldByName( "ID")->AsInteger;
   firm->name = query->FieldByName( "NAME")->AsString;
   firm->address = query->FieldByName( "ADDRESS")->AsString;
   firm->phone= query->FieldByName( "PHONE")->AsString;
}

void TGlassesBridge :: GetFirms( TStrings * firms)
{
   __int64 id;

   qryGetFirms->Open();

   try
   {
      for( int i = 0; i < firms->Count; i++)
         delete firms->Objects[ i];
      firms->Clear();

      qryGetFirms->First();

      while ( !qryGetFirms->Eof)
      {
         TGlassesFirm * firm = new TGlassesFirm();
         SetFirm(qryGetFirms, firm);
         firms->AddObject( firm->name, firm);
         qryGetFirms->Next();
      }
   }
   __finally
   {
      qryGetFirms->Close();
   }
}

void SetModel(TQuery* query, TGlassesModel* model)
{
   model->firmID = query->FieldByName( "FIRMID")->AsInteger;
   model->name   = query->FieldByName( "MODEL")->AsString;
   model->price  = (double)query->FieldByName( "PRICE")->AsCurrency;
   model->bought = query->FieldByName( "BOUGHT")->AsInteger;
   model->sold   = query->FieldByName( "SOLD")->AsInteger;
}

void TGlassesBridge :: GetModels( __int64 id, TStrings * models)
{
   __int64 firmID;
   float price;
   int bought, sold;

   qryGetModels->ParamByName( "prmFirmID")->AsInteger = id;
   qryGetModels->Open();

   try
   {
      for( int i = 0; i < models->Count; i++)
         delete models->Objects[ i];
      models->Clear();

      qryGetModels->First();

      while ( !qryGetModels->Eof)
      {
         TGlassesModel * model = new TGlassesModel();
         SetModel(qryGetModels, model);
         models->AddObject( model->name, model);
         qryGetModels->Next();
      }
   }
   __finally
   {
      qryGetModels->Close();
   }           
}

void TGlassesBridge :: Add( TGlassesFirm * firm, TStrings * models)
{
   int i;
   TGlassesModel * model;
   __int64 value;

   value = sequence->GetNextValue( "GLASSES");

   qryAdd->ParamByName( "prmID")->AsInteger = value;
   qryAdd->ParamByName( "prmName")->AsString = firm->name;
   qryAdd->ParamByName( "prmAddress")->AsString = firm->address;
   qryAdd->ParamByName( "prmPhone")->AsString = firm->phone;
   qryAdd->ExecSQL();

   qryAddModel->ParamByName( "prmFirmID")->AsInteger = value;

   for( i = 0; i < models->Count; i++)
   {
      model = ( TGlassesModel *)models->Objects[ i];

      qryAddModel->ParamByName( "prmModel")->AsString = model->name;
      qryAddModel->ParamByName( "prmPrice")->AsCurrency = model->price;
      qryAddModel->ParamByName( "prmBought")->AsInteger = model->bought;
      qryAddModel->ParamByName( "prmSold")->AsInteger = model->sold;

      qryAddModel->ExecSQL();
   }
}

void TGlassesBridge :: Update( TGlassesFirm * firm, TStrings * models)
{
   Delete( firm->id);
   Add( firm, models);
}

void TGlassesBridge :: Delete( __int64 id)
{
   qryDelete->ParamByName( "prmID")->AsInteger = id;
   qryDelete->ExecSQL();
   qryDeleteModel->ParamByName( "prmFirmID")->AsInteger = id;
   qryDeleteModel->ExecSQL();
}

bool TGlassesBridge :: GetFirm(AnsiString name, TGlassesFirm* firm)
{
   bool found = false;

   qryGetFirmByName->ParamByName("prmName")->AsString = name;
   qryGetFirmByName->Open();

   try
   {
      qryGetFirmByName->First();
      found = !qryGetFirmByName->Eof;
      if (found)
      {
         SetFirm(qryGetFirmByName, firm);
      }
   }
   __finally
   {
      qryGetFirmByName->Close();
   }

   return found;
}

bool TGlassesBridge :: GetModel(__int64 firmID, AnsiString modelName, TGlassesModel* model)
{
   bool found = false;

   qryGetModelByName->ParamByName("prmFirmID")->AsInteger = firmID;
   qryGetModelByName->ParamByName("prmModelName")->AsString = modelName;
   qryGetModelByName->Open();

   try
   {
      qryGetModelByName->First();
      found = !qryGetModelByName->Eof;
      if (found)
      {
         SetModel(qryGetModelByName, model);
      }
   }
   __finally
   {
      qryGetModelByName->Close();
   }

   return found;
}

//-----------------------------------------------------------------------------

TGlassesFirm :: TGlassesFirm()
{
   id   = -1;
   name = "";
   address = "";
   phone = "";
}

TGlassesFirm :: TGlassesFirm( __int64 id, AnsiString name, AnsiString address, AnsiString phone)
{
   this->id   = id;
   this->name = name;
   this->address = address;
   this->phone = phone;
}

//-----------------------------------------------------------------------------

TGlassesModel :: TGlassesModel()
{
   firmID = -1;
   name   = "";
   price  = 0;
   bought = 0;
   sold   = 0;
}

TGlassesModel :: TGlassesModel( __int64 firmID, AnsiString name, Currency price, int bought, int sold)
{
   this->firmID = firmID;
   this->name   = name;
   this->price  = price;
   this->bought = bought;
   this->sold   = sold;
}

